package com.hongye.proprietary.service.impl;

import com.hongye.common.utils.JwtUtil;
import com.hongye.common.utils.R;
import com.hongye.common.valid.StatusValid;
import com.hongye.proprietary.config.MyRabbitConfig;
import com.hongye.proprietary.controller.vo.LoginVo;


import com.hongye.proprietary.vo.AgentCompanyVo;
import com.hongye.proprietary.vo.LeaderboardVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hongye.common.utils.PageUtils;
import com.hongye.common.utils.Query;

import com.hongye.proprietary.dao.AgentUserDao;
import com.hongye.proprietary.entity.AgentUserEntity;
import com.hongye.proprietary.service.AgentUserService;

import javax.servlet.http.HttpSession;


@Service("agentUserService")
public class AgentUserServiceImpl extends ServiceImpl<AgentUserDao, AgentUserEntity> implements AgentUserService {
    @Autowired
    private MyRabbitConfig myRabbitConfig;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private JwtUtil jwtUtil;
    @SuppressWarnings("all")
    @Autowired
    private AgentUserDao agentUserDao;
    @Autowired
    HttpSession session;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        System.out.println(params.toString());
        IPage<AgentUserEntity> page = this.page(
                new Query<AgentUserEntity>().getPage(params),
                new QueryWrapper<AgentUserEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<LeaderboardVo> leaderboardlist() {
        return baseMapper.leaderboardlist();
    }

    @Override
    public AgentUserEntity saledetails(Integer houseagent) {
        return baseMapper.selectById(houseagent);
    }

    @Override
    public AgentUserEntity rentingdetails(Integer houseagent) {
        return baseMapper.selectById(houseagent);
    }

    @Override
    public List<AgentCompanyVo> HomeCompany() {
        return baseMapper.AgentCompany();

    }

    @Override
    public void updateAgentStatus(Integer aid, Integer status) {

        AgentUserEntity agent_id = baseMapper.selectOne(new QueryWrapper<AgentUserEntity>().eq("agent_id", aid));
        @StatusValid(values = {0, 1}) Integer agentStatus = agent_id.getAgentStatus();
        if (agentStatus==status){
            if (status==1){
                status=0;
            }
            if (status==0){
                status=1;
            }
            agent_id.setAgentStatus(status);
            baseMapper.insert(agent_id);
        }


    }


    @Override
    public R sendMsg(String phone) {
        //判断手机号是否存在
        AgentUserEntity agentPhone = baseMapper.selectOne(new QueryWrapper<AgentUserEntity>().eq("agent_phone",phone));
        if (agentPhone!=null){
            if (redisTemplate.opsForValue().get(phone)!=null){
                return R.ok("短信发送频繁 请稍后再发");
            }
            return myRabbitConfig.sendMsg(phone);
        }
        return R.error("输入的手机号不存在 请重新输入");
    }

    @Override
    public R login(LoginVo loginVo) {
        if (loginVo.getPhone()!=null){
            //根据用户手机号取出验证码
            Object s = redisTemplate.opsForValue().get(loginVo.getPhone());
            AgentUserEntity agentPhone = agentUserDao.selectOne(new QueryWrapper<AgentUserEntity>().eq("agent_phone", loginVo.getPhone()));
            //判断手机号是否正确
            if (s!=null){
                //判断验证码是否正确
                if (s.equals(loginVo.getVerification())){

                    String admin = jwtUtil.createJWT(agentPhone.getAgentId().toString(), agentPhone.getAgentName(), "admin");
                    //把当前数据存入redis
                    session.setAttribute("agentlogin",agentPhone);
                    return R.ok().put("token",admin);
                }
            }
            return R.error("验证码错误 请重新输入");
        }
      return R.error("手机号不能为空");
    }

}